package com.customview.customGroup;

import android.content.Context;
import android.support.v4.view.MarginLayoutParamsCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2019/10/24 16:14
 */

/**
 * 模仿LineaLayout 垂直分布的布局
 */
public class customGroup extends ViewGroup {
    public final static String TAG = customGroup.class.getName();

    public customGroup(Context context) {
        this(context, null);
    }

    public customGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public customGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int modewidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizewidth = MeasureSpec.getSize(widthMeasureSpec);

        int modeheigth = MeasureSpec.getMode(heightMeasureSpec);
        int sizeheigth = MeasureSpec.getSize(heightMeasureSpec);

        this.measureChildren(widthMeasureSpec, heightMeasureSpec);

        int childCount = getChildCount();

        //如果高度wrap-layout   返回的  MeasureSpec.UNSPECIFIED 无限大模式  ，因为 高度是没有无限大的
        //如果宽度wrap-layout   返回的  MeasureSpec.EXACTLY 精确模式  ，因为 宽度是有限的
        if (modeheigth == MeasureSpec.UNSPECIFIED) {

            int tmpSizeHeight = 0;
            for (int i = 0; i < childCount; i++) {
                //高度
                View childAt = getChildAt(i);
                MarginLayoutParams layoutParams = (MarginLayoutParams) childAt.getLayoutParams();
                int marginheight = layoutParams.bottomMargin + layoutParams.topMargin;
                tmpSizeHeight += childAt.getMeasuredHeight() + marginheight;

            }
            sizeheigth = tmpSizeHeight;


        }

        //宽度没有设置大小  获取 子view 最宽一个

        if (modewidth == MeasureSpec.AT_MOST) {
            int tmpMaxWidth = getSuggestedMinimumWidth();
            for (int i = 0; i < childCount; i++) {
                View childAt = getChildAt(i);
                MarginLayoutParams layoutParams = (MarginLayoutParams) childAt.getLayoutParams();

                tmpMaxWidth = Math.max(tmpMaxWidth, childAt.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin);
            }
            sizewidth = tmpMaxWidth;
        }

        setMeasuredDimension(sizewidth, sizeheigth);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        int measuredHeight = getMeasuredHeight();
        int measuredWidth = getMeasuredWidth();
        Log.i("test", "onSizeChanged: height" + measuredHeight + "\n" +
                "width: " + measuredWidth
        );
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int childCount = getChildCount();
        int left = 0;
        int right = 0;
        int top = 0;
        int bottom = 0;
        for (int i = 0; i < childCount; i++) {

            View childAt = getChildAt(i);

            MarginLayoutParams layoutParams = (MarginLayoutParams) childAt.getLayoutParams();


            int measuredHeight = childAt.getMeasuredHeight();
            right = childAt.getMeasuredWidth() + layoutParams.leftMargin;
            bottom += childAt.getMeasuredHeight() + layoutParams.topMargin;
            left = layoutParams.leftMargin;
            top += layoutParams.topMargin;

            Log.i(TAG, "onLayout: 第一个子view" + "\n left:" + left
                    + "\n top:" + top + "\n"
                    + "\n right:" + right + "\n"
                    + "\n bottom:" + bottom + "\n"
                    + "\n measuredHeight:" + measuredHeight + "\n"
            );
            childAt.layout(left, top, right, bottom);
            top += childAt.getMeasuredHeight() + layoutParams.bottomMargin;
            bottom += layoutParams.bottomMargin;

        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
}
